home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 090 / byte0387.arc / DAWSON.ARC / SIMUTIL.C < prev   
Encoding:
C/C++ Source or Header  |  1985-07-12  |  5.0 KB  |  197 lines

  1. /**********************************************\
  2. *                            *
  3. *   simutil.c = Utility operations for SIMPP:  *
  4. *   Simple IMage Processing Package.           *
  5. *   Copyright (c) 1987, Benjamin M. Dawson     *
  6. *      Edit Version: 1.2 : Jan-30-87           *
  7. *                            *
  8. \**********************************************/
  9.  
  10. #include <stdio.h>
  11. #include "simpp.h"
  12.  
  13. extern char *malloc();
  14.  
  15. /* These flags are for open() to indicate read/write ability on a file.
  16.  * The are defined for Berkeley UNIX, but may have to be changed for your
  17.  * system.
  18.  */
  19. #define READ_ONLY  00000
  20. #define WRITE_ONLY 01001
  21.  
  22. /* clear_area = Clear an area , begining at x,y and of size dx,dy to
  23.  * value z.
  24.  */
  25. int clear_area(x,y,dx,dy,z)
  26. int x,y;            /* Start of area to set */
  27. int dx,dy;            /* Size of area to set */
  28. PIXEL z;            /* Value to set area to */
  29. {
  30.     register int i;
  31.     register PIXEL *bp;
  32.     PIXEL *buf;
  33.  
  34. #ifdef CHECK
  35.     if (check_area(x,y,dx,dy,"<clear_area>") == ERROR)
  36.         return(ERROR);
  37. #endif
  38. /* Set up a buffer with size of dx */
  39.     buf = bp = (PIXEL *)malloc(dx*sizeof(PIXEL));
  40. /* Set it to all z values */
  41.     for (i = 0 ; i < dx ; i++) *bp++ = z;
  42. /* Clear lines */
  43.     while (dy--) write_hline(x,y++,dx,buf);
  44.  
  45.     free(buf);
  46.     return(OK);
  47. }
  48.  
  49. /* ================================================================ */
  50.  
  51. /* save_image = Save the image starting at x,y and of size dx,dy into the
  52.  * filewith name name.
  53.  */
  54. int save_image(x,y,dx,dy,name)
  55. int x,y;            /* Start of area to save */
  56. int dx,dy;            /* Size of area to save */
  57. char *name;            /* File name to use for save */
  58. {
  59.     int fd;
  60.     short xx,yy,dxx,dyy;    /* Shorts for header */
  61.     PIXEL *buf, *bp;
  62.     int wrsize;
  63.  
  64. #ifdef CHECK
  65.     if (check_area(x,y,dx,dy,"<save_image>") == ERROR)
  66.         return(ERROR);
  67. #endif
  68.  
  69. /* Copy integer values to shorts, so that the header is transportable
  70.    between machines */
  71.     xx = (short)x;    yy = (short)y;
  72.     dxx = (short)dx;  dyy = (short)dy;
  73.  
  74. /* Open file for writing */
  75.     if ((fd = open(name,WRITE_ONLY,0777)) < 0) {    /* Write only open */
  76.         printf("<save_image> Can't open file %s !\n",name);
  77.         perror("=");
  78.         return(ERROR);
  79.     }
  80.  
  81. /* Write out header -- xx,yy, dxx,dyy */
  82.     write(fd,&xx,sizeof(short));
  83.     write(fd,&yy,sizeof(short));
  84.     write(fd,&dxx,sizeof(short));
  85.     write(fd,&dyy,sizeof(short));
  86.  
  87.  
  88. /* Allocate a buffer */
  89.     bp = buf = (PIXEL *)malloc(dx*sizeof(PIXEL));
  90. /* Write out image data */
  91.     wrsize = dx*sizeof(PIXEL);
  92.     while (dy--) {
  93.         read_hline(x,y++,dx,bp);
  94.         if (write(fd,bp,wrsize) < wrsize) {
  95.             printf("<save_image> File write error!\n");
  96.             close(fd);
  97.             return(ERROR);
  98.         }
  99.     }
  100.  
  101.     close(fd);        /* Close file */
  102.     free(buf);        /* Free buffer */
  103.     return(OK);
  104. }        
  105.  
  106. /* ================================================================ */
  107.  
  108. /* read_image = Read an image from disk storage.  If any of the
  109.  * values x,y,dx,dy are < 0, then the corresponding value from the
  110.  * file is used.
  111.  */
  112. int read_image(x,y,dx,dy,name)
  113. int x,y;            /* Start of area to read */
  114. int dx,dy;            /* Size of area to read */
  115. char *name;            /* File name to use for read */
  116. {
  117.     int fd;
  118.     short xx,yy,dxx,dyy;    /* Shorts for header */
  119.     PIXEL *buf, *bp;
  120.     int rdsize;
  121.  
  122. /* Open file for reading */
  123.     if ((fd = open(name,READ_ONLY,0777)) < 0) {    /* Open read only */
  124.         printf("<read_image> Can't open file %s !\n",name);
  125.         return(ERROR);
  126.     }
  127.  
  128. /* Read header -- xx,yy, dxx,dyy */
  129.     read(fd,&xx,sizeof(short));
  130.     read(fd,&yy,sizeof(short));
  131.     read(fd,&dxx,sizeof(short));
  132.     read(fd,&dyy,sizeof(short));
  133.  
  134. /* Check if you should use these values or the argument values */
  135.  
  136.     if (x < 0 ) x = (int)xx;
  137.     if (y < 0 ) y = (int)yy;
  138.     if (dx < 0 ) dx = (int)dxx;
  139.     if (dy < 0) dy = (int)dyy;
  140.  
  141. /* See if you should truncate the size */
  142.  
  143.     if ((int)dxx < dx) dx = (int)dxx;
  144.     if ((int)dyy < dy) dy = (int)dyy;
  145.  
  146. /* See if it will run off the screen */
  147.  
  148.     if ((x + dx) > XSIZE) dx = XSIZE - x;
  149.     if ((y + dy) > YSIZE) dy = YSIZE - y;
  150.  
  151. /* Allocate a buffer */
  152.     bp = buf = (PIXEL *)malloc(dx*sizeof(PIXEL));
  153.  
  154. /* Read in image data */
  155.     rdsize = (int)dxx*sizeof(PIXEL);
  156.     while (dy--) {
  157.         if (read(fd,bp,rdsize) < rdsize) {
  158.             printf("<read_image> Image read error!\n");
  159.             close(fd);
  160.             return(ERROR);
  161.         }
  162.         write_hline(x,y++,dx,bp);
  163.     }
  164.  
  165.     close(fd);        /* Close file */
  166.     free(buf);        /* Free buffer */
  167.     return(OK);
  168. }        
  169.  
  170. /* ================================================================ */
  171.  
  172. /* draw_grid = Cover the screen with a grid of spacing dx,dy, and value z */
  173. VOID draw_grid(dx,dy,z)
  174. int dx,dy;        /* Grid spacing */
  175. PIXEL z;
  176. {
  177.     register int i;
  178.     register PIXEL *bp;
  179.     PIXEL *buf;
  180.  
  181. /* Which direction, horizontal or vertical, is longer? */
  182.     if (XSIZE > YSIZE) i = XSIZE;
  183.     else i = YSIZE;
  184. /* Allocate a buffer of this size, and fill it with value z */
  185.     buf = bp = (PIXEL *)malloc(i*sizeof(PIXEL));
  186.     while (i--) *bp++ = z;
  187. /* Draw the lines */
  188.     for (i = 0 ; i < YSIZE ; i += dy) write_hline(0,i,XSIZE,buf);
  189.     for (i = 0 ; i < XSIZE ; i += dx) write_vline(i,0,YSIZE,buf);
  190.  
  191.     free(buf);
  192. }
  193.  
  194. /* ================ End of simutil.c ================ */
  195.  
  196. /* <-- FILE BREAK --> */
  197.